package com.bw.shop2208a.utils

import android.content.Context
import com.alibaba.android.arouter.facade.Postcard
import com.alibaba.android.arouter.facade.annotation.Interceptor
import com.alibaba.android.arouter.facade.callback.InterceptorCallback
import com.alibaba.android.arouter.facade.template.IInterceptor
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils

/**
 * 登录拦截器 ---ARouter拦截器  添加注解  设置优先级  数越小优先级越高
 */
@Interceptor(priority = 1)
class LoginIntercepter : IInterceptor {
    override fun init(context: Context?) {

    }

    /**
     * postcard:路由信息 包含跳转的路径信息
     * InterceptorCallback:拦截器回调事件
     */
    override fun process(postcard: Postcard?, callback: InterceptorCallback?) {
        if ("/app/login".equals(postcard?.path) || "/app/register".equals(postcard?.path) || SPUtils.getInstance()
                .getString(Constant.SP_TOKEN).isNotEmpty()
        ) {
            //当跳转到登录界面或者注册界面时，可直接向下进行
            //如果当前账户一登陆，其他路由信息也可以继续执行
            callback?.onContinue(postcard)
        } else {
            ToastUtils.showLong("您未登录")
            ARouter.getInstance().build("/app/login").navigation()
        }
    }

}